package com.shop.service.book;

import com.mysql.jdbc.StringUtils;
import com.shop.dao.BaseDao;
import com.shop.dao.book.BookDaoImpl;
import com.shop.dao.book.IBookDao;
import com.shop.pajo.Book;

import java.io.File;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class BookServiceImpl implements IBookService{
    private IBookDao bookDao = null;
    public BookServiceImpl(){
        bookDao = new BookDaoImpl();
    }
    @Override
    public List<Book> query(String queryName, String queryCode) {
        Connection connection = null;
        List<Book> bookList = null;
        try {
            connection = BaseDao.getConnection();
            bookList = bookDao.query(connection, queryName ,queryCode);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection,null,null);
        }
        return bookList;
    }

    @Override
    public int add(Book book) {
        int addNum = 0;
        Connection connection = null;
        connection = BaseDao.getConnection();
        try {
            connection.setAutoCommit(false);
            addNum = bookDao.add(connection,book);
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return addNum;
    }

    @Override
    public Book getBookById(String id) {
        Connection connection = null;
        Book book = null;
        connection = BaseDao.getConnection();
        try {
            book = bookDao.getBookById(connection,id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return book;
    }

    @Override
    public int update(Book book,String realPath) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        String picturePath = null;
        String deletePath = null;
        File file = null;
        int bookNum = 0;
        //修改图片的时候，如果这里为空，则证明没有修改，这时候通过id查找对应的book，
        // 取其图片值赋值给这个空的picture，保证更新不修改图书值
        if(book.getPicture() == null){
            try {
                book.setPicture(bookDao.getBookById(connection,book.getId()).getPicture());
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }else{
            try {
                picturePath = bookDao.getBookById(connection, book.getId()).getPicture();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            if(!StringUtils.isNullOrEmpty(picturePath)){
                deletePath = realPath + "/" + picturePath;
                file = new File(deletePath);
            }
        }
        try {
            connection.setAutoCommit(false);
            bookNum = bookDao.updateBook(connection, book);
            connection.commit();
            if(bookNum>0 && deletePath != null){
                file.delete();
            }
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return bookNum;
    }

    @Override
    public int delete(String id,String realPath) {
        Connection connection = null;
        int bookNum = 0;
        String picturePath = null;
        String deletePath = null;
        File file = null;
        try {
            connection = BaseDao.getConnection();
            connection.setAutoCommit(false);
            picturePath = bookDao.getBookById(connection, id).getPicture();
            if(!StringUtils.isNullOrEmpty(picturePath)){
                deletePath = realPath + "/" + picturePath;
                file = new File(deletePath);
            }
            bookNum = bookDao.deleteBookById(connection, id);
            connection.commit();
            if(bookNum>0 && deletePath != null){
                file.delete();
            }
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                connection.rollback();//事务回滚
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
        return bookNum;
    }

    @Override
    public List<Book> queryByClass(String bClass) {
        Connection connection = null;
        List<Book> bookList = null;
        try {
            connection = BaseDao.getConnection();
            bookList = bookDao.queryByClass(connection,bClass);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection,null,null);
        }
        return bookList;
    }
}
